<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

include( './admin/model/category.class.php' );
$m_cat = new Category();

if( $action == 'mag' )
{
	if( $do == 'list' )
	{
		$category = $m_cat->get_category( true );
		include( display( 'category_list' ) );
		unset( $category );
	}
}
elseif( $action == 'new' )
{
	if( $do == 'newarcat' )
	{
		
	}
}
elseif( $action == 'addcol' )
{
	$pid = get_post( 'pid' );
	$pid = empty( $pid )?0:$pid;
	$default_temp = $m_cat->get_default_temp();
	$temp_arr = $m_cat->get_template();
	$index_temp = $temp_arr['index_temp'];
	$list_temp = $temp_arr['list_temp'];
	$article_temp = $temp_arr['article_temp'];
	$member_group = $m_cat->get_member_group();
	$column_tree = $m_cat->get_column_tree();
	include( display( 'admin/ar_add_column' ) );
	unset( $default_temp, $temp_arr, $index_temp, $list_temp, $article_temp, $member_group, $column );
}
elseif( $action == 'addcolumn' )
{
	$datas = array();
	$datas['name'] = get_post( 'name' );
	$datas['pid'] = get_post( 'pid' );
	$datas['ifshow'] = get_post( 'ifshow' );
	$datas['ifpost'] = get_post( 'ifpost' );
	$datas['comment'] = get_post( 'comment' );
	$datas['attribute'] = get_post( 'attribute' );
	$datas['html_path'] = get_post( 'html_path' );
	$datas['keywords'] = get_post( 'keywords' );
	$datas['description'] = get_post( 'description' );
	$datas['order'] = get_post( 'order' );
	$datas['jump'] = get_post( 'jump' );
	$datas['index_temp'] = get_post( 'index_temp' );
	$datas['list_temp'] = get_post( 'list_temp' );
	$datas['article_temp'] = get_post( 'article_temp' );
	$datas['view_group'] = get_post( 'view_group' );
	$datas['post_group'] = get_post( 'post_group' );
	$id = $m_cat->add_column( $datas );
	if( $id )
	{
		$success = '<a href="admincp.php?m=arcolumn&a=addcol">继续添加文章分类</a>
					<a href="admincp.php?m=arcolumn&a=index">返回分类列表</a>
					<a href="admincp.php?m=arcolumn&a=edit&id='.$id.'">点击修改</a>
					<a href="arcolumn.php?id='.$id.'">查看本分类</a>';
		jump( $success, 'admincp.php?m=arcolumn&a=addcol' );
	}
	else 
	{
		alert( '文章分类添加失败，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'edit' )
{
	$id = get_post( 'id' );
	$temp_arr = $m_cat->get_template();
	$index_temp = $temp_arr['index_temp'];
	$list_temp = $temp_arr['list_temp'];
	$article_temp = $temp_arr['article_temp'];
	$column_tree = $m_cat->get_column_tree();
	$member_group = $m_cat->get_member_group();
	$column = $m_cat->get_column( $id );
	$views = explode( ' ', $column['view_group'] );
	$posts = explode( ' ', $column['post_group'] );
	$view_group = '';
	$post_group = '';
	$member = array();
	foreach( $member_group as $v )
	{
		$member[$v['name']] = $v['id'];
	}
	foreach( $member as $k2 => $v2 )
	{
		if( in_array( $v2, $views ) )
		{
			$view_group .= '<input type="checkbox" name="view_group[]" value="'.$v2['id'].'" checked>'.$k2.'&nbsp;&nbsp;';
		}
		else 
		{
			$view_group .= '<input type="checkbox" name="view_group[]" value="'.$v2['id'].'">'.$k2.'&nbsp;&nbsp;';
		}
	}
	unset( $column['view_group'] );
	foreach( $member as $k3 => $v3 )
	{
		if( in_array( $v3, $posts ) )
		{
			$post_group .= '<input type="checkbox" name="post_group[]" value="'.$v3['id'].'" checked>'.$k3.'&nbsp;&nbsp;';
		}
		else 
		{
			$post_group .= '<input type="checkbox" name="post_group[]" value="'.$v3['id'].'">'.$k3.'&nbsp;&nbsp;';
		}
	}
	unset( $column['post_group'] );
	include( display( 'admin/ar_edit_column' ) );
	unset( $id, $column );
}
elseif( $action == 'update' )
{
	$id = get_post( 'id' );
	$pid = get_post( 'pid' );
	if( $id == $pid || $m_cat->check_pid( $id, $pid ) )
	{
		alert( '不允许从本类高级分类向低级分类或自己移动！' );
		go_back();exit;
	}
	$datas = array();
	$datas['name'] = get_post( 'name' );
	$datas['pid'] = $pid;
	$datas['ifshow'] = get_post( 'ifshow' );
	$datas['ifpost'] = get_post( 'ifpost' );
	$datas['comment'] = get_post( 'comment' );
	$datas['attribute'] = get_post( 'attribute' );
	$datas['html_path'] = get_post( 'html_path' );
	$datas['keywords'] = get_post( 'keywords' );
	$datas['description'] = get_post( 'description' );
	$datas['order'] = get_post( 'order' );
	$datas['jump'] = get_post( 'jump' );
	$datas['index_temp'] = get_post( 'index_temp' );
	$datas['list_temp'] = get_post( 'list_temp' );
	$datas['article_temp'] = get_post( 'article_temp' );
	$datas['view_group'] = get_post( 'view_group' );
	$datas['post_group'] = get_post( 'post_group' );
	if( $m_cat->update_column( $id, $datas ) )
	{
		$success = '<a href="admincp.php?m=arcolumn&a=edit&id='.$id.'">继续编辑该分类</a>
					<a href="admincp.php?m=arcolumn&a=addcol">添加新分类</a>
					<a href="admincp.php?m=arcolumn&a=index">返回分类列表</a>
					<a href="arcolumn.php?id='.$id.'">查看本分类</a>';
		jump( $success, 'admincp.php?m=arcolumn&a=index' );
	}
	else 
	{
		alert( '文章分类修改失败，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'checkcolumn' )
{
	$id = get_post( 'id' );
	echo $m_cat->check_column( $id )?"{'ok':1}":"{'ok':0}";
}
elseif( $action == 'checkmove' )
{
	$id = get_post( 'id' );
	$pid = get_post( 'pid' );
	echo ( $id == $pid || $m_cat->check_pid( $id, $pid ) )?"{'ok':0}":"{'ok':1}";
	unset( $id, $pid );
}
elseif( $action == 'move' )
{
	$id = get_post( 'id' );
	$pid = get_post( 'pid' );
	if( $m_cat->move( $id, $pid ) )
	{
		jump( 'SunCMS提示：栏目移动成功！', 'admincp.php?m=arcolumn&a=index', 1 );
	}
	else 
	{
		alert( '栏目移动失败，请联系管理员！' );
		go_back();
	}
	unset( $id, $pid );
}
elseif( $action == 'delete' )
{
	$id = get_post( 'id' );
	$result = $m_cat->delete( $id );
	$array = array(
			'ok' => 1,
			'ids' => $result?$result:array()
	);
	echo json_encode( $array );
}
elseif( $action == 'updateorder' )
{
	$datas = get_post( 'datas' );
	$datas = json_decode( stripslashes( $datas ), true );
	echo $m_cat->update_order( $datas )?"{'ok':1}":"{'ok':0}";
}

unset( $m_cat );